package com.atguigu.lease.admin.controller.user;

import com.atguigu.lease.admin.service.UserInfoService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.bean.UserInfo;
import com.atguigu.lease.model.vo.user.UserInfoQueryVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Tag(name="平台用户管理模块")
@RestController
@RequestMapping("/admin/user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @Operation(summary = "带条件分页查询平台用户列表")
    @GetMapping("/page")
    public Result<Page<UserInfo>> pageItem(@RequestParam Long current,
                                           @RequestParam Long size,
                                           UserInfoQueryVo queryVo){
        Page<UserInfo> page = userInfoService.page(new Page<UserInfo>(current, size),
                Wrappers.lambdaQuery(UserInfo.class)
                        .eq(!StringUtils.isEmpty(queryVo.getStatus()),
                                UserInfo::getStatus, queryVo.getStatus())
                        .like(!StringUtils.isEmpty(queryVo.getPhone()),
                                UserInfo::getPhone, queryVo.getPhone()));
        return Result.ok(page);
    }

    @Operation(summary = "根据id更新平台用户的状态")
    @PostMapping("/updateStatusById")
    public Result pageItem(@RequestParam Long id ,
                                           @RequestParam String status){
        userInfoService.update(Wrappers.lambdaUpdate(UserInfo.class)
                .eq(UserInfo::getId, id)
                .set(UserInfo::getStatus, status));

//        UserInfo userInfo = new UserInfo();
//        userInfo.setId(id);
//        userInfo.setStatus(Integer.parseInt(status));
//        userInfoService.updateById(userInfo);  mp updateById方法传入对象更新时，自动使用id当做条件，更新非空属性

        return Result.ok();
    }
}
